/*
 * @Author: song
 * @Description: 技能编辑配置
 * @Date: 2024年04月28日 星期日 16:04:50
 * @Modify:
 */

using System;
using System.Collections.Generic;
using SCore.Editor.WindowDraw;
using Function.SerializedPropertyAttribute;
using Function.SkillEditor.Runtime;
using UnityEditor;
using UnityEngine;

namespace Function.SkillEditor.Editor
{
    [Serializable]
    public class SkillConfigItemData : NCommonView, IInspector
    {
        public string _name = string.Empty;
        public int _id = 0;
        public SkillRunLine _skillRunLine;
        public GameObject _editorGo;

        private GUIStyle _selectGUIStyle;
        private GUIStyle _notSelectGUIStyle;

        public SkillConfigPageView GetPageView()
        {
            var bindViews = SkillEditorConfig.Instance.GetWindowConfig(SkillWindowType.Config);
            return bindViews._eCommonViews[0] as SkillConfigPageView;
        }

        public override void OnGUI()
        {
            if (_selectGUIStyle == null)
            {
                _selectGUIStyle =
                    EditorDrawHelp.GetTextGUIStyle(15, TextAnchor.MiddleCenter, GetPageView()._selectTextColor);
            }

            if (_notSelectGUIStyle == null)
            {
                _notSelectGUIStyle =
                    EditorDrawHelp.GetTextGUIStyle(15, TextAnchor.MiddleCenter, GetPageView()._textColor);
            }

            var curItemView = GetPageView().GetCurSelect();
            var curStyle = curItemView == this ? _selectGUIStyle : _notSelectGUIStyle;
            GUILayout.Label($"{_name}_{_id}", curStyle);
        }

        public void OnInspector(Rect rect)
        {
            GUILayout.BeginArea(new Rect(rect.x, rect.y, rect.width, 20));
            _name = EditorDrawHelp.DrawStringField("名字",
                _name ?? string.Empty, rect.width);
            GUILayout.EndArea();


            GUILayout.BeginArea(new Rect(rect.x, rect.y + 22, rect.width, 20));
            EditorGUI.BeginChangeCheck();
            var editorGo =
                EditorDrawHelp.DrawHObjetField("物体", typeof(GameObject), _editorGo, rect.width) as GameObject;
            if (EditorGUI.EndChangeCheck())
            {
                _editorGo = editorGo;
                SkillEditorWindow.EventManager.BroadCast(SkillEditorEventType.EditorGameObjectChange, _editorGo);
            }

            GUILayout.EndArea();

            GUILayout.BeginArea(new Rect(rect.x, rect.y + 44, rect.width, 20));
            if (GUI.Button(new Rect(0, 0, rect.width, 20), "删除"))
            {
                GetPageView().DelItem(this);
                SkillEditorWindow.EventManager.BroadCast<IInspectorType, IInspector>(
                    SkillEditorEventType.InspectorTargetChange, IInspectorType.Up, null);
                SkillEditorWindow.EventManager.BroadCast<IInspectorType, IInspector>(
                    SkillEditorEventType.InspectorTargetChange, IInspectorType.Down, null);
                SkillEditorWindow.EventManager.BroadCast<SkillConfigItemData>(
                    SkillEditorEventType.TimeLineEditChange,
                    null);
            }

            GUILayout.EndArea();
        }

        protected override bool MouseDown(Event evt)
        {
            SkillEditorWindow.EventManager.BroadCast<IInspectorType, IInspector>(
                SkillEditorEventType.InspectorTargetChange, IInspectorType.Up, this);
            SkillEditorWindow.EventManager.BroadCast<IInspectorType, IInspector>(
                SkillEditorEventType.InspectorTargetChange, IInspectorType.Down, null);
            SkillEditorWindow.EventManager.BroadCast(
                SkillEditorEventType.TimeLineEditChange, this);
            GetPageView().SelectItem(this);
            return true;
        }

        protected override bool ContextClick(Event evt)
        {
            return true;
        }

        public override void UpdateEvent(Event evt)
        {
            //keyDown
            if (evt.type == EventType.KeyDown && evt.keyCode == KeyCode.Delete)
            {
                var curItemView = GetPageView().GetCurSelect();
                if (curItemView == this && _rect.Contains(evt.mousePosition))
                {
                    GetPageView().DelItem(this);
                    SkillEditorWindow.EventManager.BroadCast<IInspectorType, IInspector>(
                        SkillEditorEventType.InspectorTargetChange, IInspectorType.Up, null);
                    SkillEditorWindow.EventManager.BroadCast<IInspectorType, IInspector>(
                        SkillEditorEventType.InspectorTargetChange, IInspectorType.Down, null);
                    SkillEditorWindow.EventManager.BroadCast<SkillConfigItemData>(
                        SkillEditorEventType.TimeLineEditChange,
                        null);
                }
            }

            base.UpdateEvent(evt);
        }

        public int GetEndFrame()
        {
            if (_skillRunLine != null)
            {
                return _skillRunLine._lengthFrame;
            }

            return 0;
        }

        public List<SkillTrack> GetSkillTracks()
        {
            return _skillRunLine._skillTracks.FindAll((skillTrack) => { return skillTrack != null; });
        }

        public static SkillConfigItemData Copy(SkillConfigItemData copyData)
        {
            SkillConfigItemData itemData = new SkillConfigItemData();
            itemData._id = copyData._id;
            itemData._name = copyData._name;
            itemData._editorGo = copyData._editorGo;
            itemData._skillRunLine = copyData._skillRunLine;
            return itemData;
        }
    }


    [CustomEditor(typeof(SkillConfigPageView))]
    public class SkillConfigPageViewEditor : ObjectDrawEditor<SkillConfigPageView>
    {
    }

    [CreateAssetMenu(fileName = "SkillConfigPageView", menuName = "ScriptableObject/SkillEditor/SkillConfigPage",
        order = 3)]
    public class SkillConfigPageView : EPageView<SkillConfigItemData>
    {
        protected override List<SkillConfigItemData> GetPageMap()
        {
            return SkillEditorWindow._config.GetMap();
        }

        protected override List<SkillConfigItemData> GetShowTime(List<SkillConfigItemData> pageMap, string search)
        {
            List<SkillConfigItemData> showMap = new List<SkillConfigItemData>();
            if (!string.IsNullOrEmpty(_searchText))
            {
                for (int index = 0; index <= pageMap.Count - 1; index++)
                {
                    var pageItem = pageMap[index] as SkillConfigItemData;
                    if (pageItem._name != null &&
                        pageItem._name.IndexOf(_searchText, StringComparison.OrdinalIgnoreCase) >= 0)
                    {
                        showMap.Add(pageMap[index]);
                    }
                    else if (pageItem._id.ToString().IndexOf(_searchText, StringComparison.OrdinalIgnoreCase) >=
                             0)
                    {
                        showMap.Add(pageMap[index]);
                    }
                }
            }
            else
            {
                foreach (var item in pageMap)
                {
                    showMap.Add(item);
                }
            }

            return showMap;
        }

        protected override bool AddItem(int id)
        {
            foreach (var config in _pageMap)
            {
                if (config._id == id)
                {
                    return false;
                }
            }

            SkillEditorWindow._config.AddMap(id);
            return true;
        }
    }
}